package org.projects.graduates.domain;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import com.dayatang.domain.AbstractEntity;
import com.dayatang.domain.QuerySettings;

/**
 * 排课
 * 
 * @author Administrator
 * 
 */
@Entity
@Table(name = "time_tables")
public class TimeTable extends AbstractEntity {

	private static final long serialVersionUID = -3266108021220007008L;

	// 课程
	@ManyToOne
	private Course course;

	// 交的时间
	@Temporal(TemporalType.DATE)
	private Date teachDate;

	@OneToOne
	private Content content;

	@Temporal(TemporalType.DATE)
	private Date lastModify;

	@Enumerated(EnumType.STRING)
	private ContentStatus status;

	public TimeTable() {
		super();
	}

	public TimeTable(Course course, Date teachDate) {
		super();
		this.course = course;
		this.teachDate = teachDate;
	}

	public boolean isNullContent() {
		return null == content || this.content.getTxt() == null || StringUtils.isEmpty(this.content.getTxt());
	}

	public static List<TimeTable> findBy(Course course) {
		List<TimeTable> results = new ArrayList<TimeTable>();
		QuerySettings<TimeTable> settings = QuerySettings.create(TimeTable.class);
		settings.eq("course", course);
		results.addAll(getRepository().find(settings));
		return results;
	}

	public static List<TimeTable> findBy(long courseId) {
		List<TimeTable> results = new ArrayList<TimeTable>();
		QuerySettings<TimeTable> settings = QuerySettings.create(TimeTable.class);
		settings.eq("course.id", courseId);
		results.addAll(getRepository().find(settings));
		return results;
	}

	public static List<Date> allDates(long courseId) {
		List<Date> results = new ArrayList<Date>();
		QuerySettings<TimeTable> settings = QuerySettings.create(TimeTable.class);
		settings.eq("course.id", courseId);
		for (TimeTable tt : getRepository().find(settings)) {
			results.add(tt.getTeachDate());
		}
		return results;
	}

	// 找到今日的课程
	/**
	 * 
	 * 
	 * 这个方法有bug，没法根据new Date()查到当日的课程内容
	 * 
	 * **/
	public static TimeTable getToday(Course course) {

		QuerySettings<TimeTable> settings = QuerySettings.create(TimeTable.class);
		settings.eq("course", course).eq("status", ContentStatus.ARTWORK).eq("teachDate", getToday());
		TimeTable tt = getRepository().getSingleResult(settings);
		return tt;
	}

	private static Date getToday() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String todayStr = sdf.format(new Date());
		Date today = null;
		try {
			today = sdf.parse(todayStr + " 00:00:00");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("TimeTable today : " + today);
		return today;
	}

	public boolean equals(Object other) {
		if (null == other) {
			return false;
		}
		if (other instanceof TimeTable) {
			TimeTable o = (TimeTable) other;
			if (o.getId() == this.getId()) {
				return true;
			}
		}
		return false;
	}

	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(this.getId()).toHashCode();
	}

	public String toString() {
		return this.teachDate + "  开课 ";
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	public Date getTeachDate() {
		return teachDate;
	}

	public void setTeachDate(Date teachDate) {
		this.teachDate = teachDate;
	}

	public Content getContent() {
		return content;
	}

	public void setContent(Content content) {
		this.content = content;
	}

	public Date getLastModify() {
		return lastModify;
	}

	public void setLastModify(Date lastModify) {
		this.lastModify = lastModify;
	}

	public ContentStatus getStatus() {
		return status;
	}

	public void setStatus(ContentStatus status) {
		this.status = status;
	}

}
